//
//  JumpHeadView.m
//  Item A House
//
//  Created by dlios on 15/5/25.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "JumpHeadView.h"
#import "SANetWorkingTask.h"
#import <UIImageView+WebCache.h>
#import "RankModel.h"
@interface JumpHeadView ()<UIScrollViewDelegate>

@end
@implementation JumpHeadView

- (void)dealloc
{
    [_normalArray release];
    [_imageArray release];
    [_scroll release];
    [_timer release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.imageArray = [NSMutableArray array];
        self.normalArray = [NSMutableArray array];
        self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, ([UIScreen mainScreen].bounds.size.width * 0.5))];
        self.scroll.delegate = self;
        self.scroll.bounces = NO;
        self.scroll.pagingEnabled = YES;
        [self addSubview:self.scroll];
        [self.scroll release];
    }
    return self;
}
#pragma mark -
#pragma mark scrollView的headView
- (void)setid:(NSString *)loupanid
{
    NSDictionary *dic = @{@"loupan_id":loupanid};
    [SANetWorkingTask request:@"http://api.anjuke.com/xinfang/m/iphone/1.3/loupan/Newimages/" parmater:dic block:^(NSDictionary *result) {
        NSArray *arr1 = [result objectForKey:@"result"];
        NSDictionary *dic1 = [arr1 objectAtIndex:0];
        self.imageArray = [dic1 objectForKey:@"images"];
        //NSLog(@"%@", self.imageArray);
        //刷新?
        
        
        //欺骗
        id first = [self.imageArray firstObject];
        id last = [self.imageArray lastObject];
        [self.imageArray addObject:first];
        [self.imageArray insertObject:last atIndex:0];
        for (int i = 0; i < [self.imageArray count]; i++) {
            NSDictionary *dic = [self.imageArray objectAtIndex:i];
            NSString *str = [dic objectForKey:@"image"];
            //放图片
            UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.scroll.frame.size.width * i, 0, self.scroll.frame.size.width, self.scroll.frame.size.height)];
            myImage.userInteractionEnabled = YES;
            [myImage sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"10.jpg"]];
            [self.scroll addSubview:myImage];
            [myImage release];
            
            
        }
        [self.scroll setContentSize:CGSizeMake([self.imageArray count] * self.scroll.frame.size.width, 0)];
        [self.scroll setContentOffset:CGPointMake(self.scroll.frame.size.width, 0)];
        
        //时间3s
        if (self.timer) {
            [self.timer invalidate];
            self.timer = nil;
        }
        self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
        
    }];
    
    
    
    
}
//页数
- (void)timerAction:(NSTimer *)timer
{
    self.pageTwo++;
    CGFloat offWidth = self.scroll.frame.size.width * self.pageTwo;
    NSInteger num = self.scroll.contentSize.width / self.scroll.frame.size.width;
    
    [UIView animateWithDuration:0.5 animations:^{
        [self.scroll setContentOffset:CGPointMake(offWidth, 0)];
    }];
    
    
    
    if (self.pageTwo == num - 1) {
        self.pageTwo = 1;
        [self.scroll setContentOffset:CGPointMake(self.scroll.frame.size.width, 0)];
    }
    
    
}
//滚动结束时执行这个协议,循环跳转图片
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGSize offSize = scrollView.contentSize;
    CGPoint offSet = scrollView.contentOffset;
    CGFloat width = scrollView.frame.size.width;
    self.pageTwo = offSet.x / width;
    //如果宽度是第一张,偏移量变为倒数第二张
    if (offSet.x == 0) {
        self.pageTwo = scrollView.contentSize.width / scrollView.frame.size.width - 2;
        scrollView.contentOffset = CGPointMake(offSize.width - width * 2, 0);
    }
    //如果宽度是最后一张,则偏移量变为第二张
    if (offSet.x == offSize.width - width) {
        self.pageTwo = 1;
        scrollView.contentOffset = CGPointMake(width, 0);
    }
    
}
#pragma mark -
#pragma mark 普通区的headView
- (void)headViewNormal:(RankModel *)lanpanid
{
    //NSLog(@"%@",lanpanid.kaipan_new_date);
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, ([UIScreen mainScreen].bounds.size.width * 0.5), self.frame.size.width, 20)];
    titleLabel.text = lanpanid.loupan_name;
    [titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
    [self addSubview:titleLabel];
    [titleLabel release];
    
    UILabel *tags = [[UILabel alloc] initWithFrame:CGRectMake(10, ([UIScreen mainScreen].bounds.size.width * 0.5) + 20, self.frame.size.width, 20)];
    tags.text = lanpanid.tags;
    [tags setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
    [self addSubview:tags];
    [tags release];
    
    UILabel *price = [[UILabel alloc] initWithFrame:CGRectMake(10, ([UIScreen mainScreen].bounds.size.width * 0.5) + 40, self.frame.size.width, 20)];
    
    price.text = [NSString stringWithFormat:@"售价:%ld", (long)lanpanid.price];
    
    price.textColor = [UIColor redColor];
    price.font = [UIFont systemFontOfSize:15];
    [self addSubview:price];
    [price release];
    
    UILabel *data = [[UILabel alloc] initWithFrame:CGRectMake(10, ([UIScreen mainScreen].bounds.size.width * 0.5) + 60, self.frame.size.width, 20)];
    data.text = [NSString stringWithFormat:@"开盘日期:%@", lanpanid.kaipan_new_date];
    data.font = [UIFont systemFontOfSize:15];
    [self addSubview:data];
    [data release];
    
    UILabel *address = [[UILabel alloc] initWithFrame:CGRectMake(10, ([UIScreen mainScreen].bounds.size.width * 0.5) + 80, self.frame.size.width, 20)];
    address.text = [NSString stringWithFormat:@"所属区域:%@-%@", lanpanid.region_title, lanpanid.sub_region_title];
    address.font = [UIFont systemFontOfSize:15];
    [self addSubview:address];
    [address release];
}
@end
